#

import httplib
import json
import webob.dec
from webob import Response

class Controller(object):

    def __init__(self):
        self.version = '0.1'

    def index(self, req):
        response = Response(request=req, status=httplib.MULTIPLE_CHOICES, content_type='application/json')
        response.body = json.dumps(dict(versions=self.version))
        return response

    def default_response(self, environ, start_response):
        _headers = []
        _headers.append(('Content-Type', 'text/plain'))
        start_response('200 OK', _headers)
        return ['test wsgi hello world, default page\r\n']

    @webob.dec.wsgify
    def __call__(self, request):
        match = request.environ['wsgiorg.routing_args'][1]
        if not match:
            return self.default_response

        if match['action'] == 'index':
            return self.index(request)

        return self.default_response


def create_resource():
    return Controller()
